package com.frikinet.lifeguard.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import org.lobobrowser.html.HtmlRendererContext;
import org.w3c.dom.html2.HTMLElement;
import org.w3c.dom.html2.HTMLLinkElement;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.Lifeguard;
import com.frikinet.lifeguard.events.PostSelectedEvent;
import com.frikinet.lifeguard.events.PostSelectedListener;
import com.frikinet.lifeguard.events.UserSelectedEvent;
import com.frikinet.lifeguard.gui.dialogs.FotologLoadingDataDialog;
import com.frikinet.lifeguard.model.User;
import com.frikinet.lifeguard.util.HibernateUtil;
import com.frikinet.tools.gui.ActionHandler;
import com.frikinet.tools.gui.FMenuBuilder;
import com.frikinet.tools.gui.FPopupMenu;

public class CommentsHtmlPanel extends JPanel implements PostSelectedListener
{
	private static final long serialVersionUID = 1L;
	
	private TemplateHtmlPanel htmlPanel;
	
	public CommentsHtmlPanel()
	{
		super(new BorderLayout());
		
		App.events.addPostSelectedListener(this);
		
		buildGUI();
	}
	
	private void buildGUI()
	{
		htmlPanel = new TemplateHtmlPanel(new UserLinkBehaviour());
		add(htmlPanel, BorderLayout.CENTER);
	}

	@Override
	public void postSelected(PostSelectedEvent event)
	{
		Map<String, Object> context = new HashMap<String, Object>();
		context.put("post", event.getPost());
		htmlPanel.navigate("comments", context);
	}
	
	public class UserLinkBehaviour extends HtmlBehaviourAdapter
	{
		private FPopupMenu popup;
		private ActionHandler handler = new ActionHandler(this);
		
		String user = "";
		
		public UserLinkBehaviour()
		{
			FMenuBuilder menuBuilder = new FMenuBuilder(handler, App.i18n); 
			popup = menuBuilder.build(Lifeguard.class.getResourceAsStream("gui/popup_user.xml"), FPopupMenu.class);
			popup.translate();
		}
		
		@Override
		public boolean checkElement(HTMLElement element)
		{
			return element instanceof HTMLLinkElement && element.getAttribute("class").equals("user");
		}

		@Override
		public void performLinkClicked(HtmlRendererContext renderer, HTMLElement element, URL url, String target)
		{
			User user = HibernateUtil.get(User.class, "name", element.getTextContent().trim());
			if(user != null)
			{
				App.events.fireUserSelected(new UserSelectedEvent(this, user));
			}
		}

		@Override
		public boolean performContextMenu(HtmlRendererContext renderer, HTMLElement element, MouseEvent event)
		{
			user = element.getTextContent().trim();
			popup.show(CommentsHtmlPanel.this.htmlPanel, event.getX(), event.getY());
			return true;
		}
		
		public void action_load_user()
		{
			new FotologLoadingDataDialog(App.app, user);
		}
		
		public void action_go_user()
		{
			User user2 = HibernateUtil.get(User.class, "name", user);
			if(user2 != null)
			{
				App.events.fireUserSelected(new UserSelectedEvent(this, user2));
			}
		}
	}
}
